//
//  BlackjackGame.m
//  Blackjack
//
//  Created by mrbradle on 11/7/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "BlackjackGame.h"

@implementation BlackjackGame

@synthesize name,rounds,chips,currentChips,currentRound,currentBet,status,minBet,maxBet,currentMaxBet;
@synthesize playerCards,houseCards,currentHouseCards,currentPlayerCards,currentDeckPosition,deck;
@synthesize playerHasAce,houseHasAce,playerValue,houseValue;

- (id)initWithChips:(int)chips_ rounds:(int)rounds_ {
    
    self.playerCards = (int*)malloc(sizeof(int)*13);
    self.houseCards = (int*)malloc(sizeof(int)*13);
    self.currentPlayerCards = 0;
    self.currentHouseCards = 0;
    
    self.deck = (int*)malloc(sizeof(int)*52);
    for (int i = 0; i < 52; i++) {
        deck[i] = i;
    }
    self.currentDeckPosition = 0;
    
    self.minBet = 1;
    self.maxBet = 10;
    self.currentMaxBet = 10;
    
    self.chips = chips_;
    self.currentChips = chips_;
    self.rounds = rounds_;
    self.currentRound = 0;
    self.currentBet = 0;
    self.status = 0;
    
    return self;
    
}

- (void)shuffle {
    
    self.currentDeckPosition = 0;
    for (int i = 0; i < 52; i++) {
        int position = random()%(52-i)+i;
        int temp = deck[i];
        deck[i] = deck[position];
        deck[position] = temp;
    }
    
}

- (int)dealCard {
    currentDeckPosition++;
    return deck[currentDeckPosition-1];
}

- (void)newGame {
    
    [self newRound];
    
}

- (void)newRound {
    
    self.currentPlayerCards = 0;
    self.currentHouseCards = 0;
    
    self.currentBet = 0;
    self.playerValue = 0;
    self.houseValue = 0;
    self.currentRound++;
    [self shuffle];
    self.status = 1;
    
    self.currentMaxBet = self.maxBet;
    if (self.currentChips < self.maxBet) self.currentMaxBet = self.currentChips;
    
}

//Deals two cards to the player and two cards to the house
- (void)deal {
    
    self.currentChips -= self.currentBet;
    self.status = 2;
    
    self.playerCards[0] = [self dealCard];
    self.houseCards[0] = [self dealCard]; //Note that this card is supposed to be hidden from the player.
    self.playerCards[1] = [self dealCard];
    self.houseCards[1] = [self dealCard];
    self.currentPlayerCards = 2;
    self.currentHouseCards = 2;
    
    self.playerHasAce = (playerCards[0] % 13 == 0|| playerCards[1] % 13 == 0);
    self.houseHasAce = houseCards[1] % 13 == 0;
    
    //Initialize the player and house values
    
    for (int i = 0; i < 2; i++) {
        int cardValue = self.playerCards[i] % 13 + 1;
        if (cardValue > 10) cardValue = 10;
        else if (cardValue == 1) cardValue = 11;
        self.playerValue += cardValue;
    }
    if (self.playerValue == 22) self.playerValue = 12;
    
    self.houseValue = self.houseCards[1] % 13 + 1;
    if (self.houseValue > 10) self.houseValue = 10;
    else if (self.houseValue == 1) self.houseValue = 11;    
    //self.playerNumAces = (playerCards[0] % 13 == 0) + (playerCards[1] % 13 == 0);
    //self.houseNumAces = (houseCards[0] % 13 == 0) + (houseCards[1] % 13 == 0);
    
}

- (void)hit {
    int card = [self dealCard];
    self.playerCards[self.currentPlayerCards] = card;
    
    int cardValue = self.playerCards[self.currentPlayerCards] % 13 + 1;
    if (cardValue > 10) cardValue = 10;
    else if (cardValue == 1) cardValue = 11;
    self.playerValue += cardValue;
    
    if (card % 13 == 0) self.playerHasAce = YES;
    
    if (self.playerValue > 21 && self.playerHasAce) {
        self.playerValue -= 10;
        self.playerHasAce = NO;
    }
        
    self.currentPlayerCards++;

    /*if (self.playerCards[self.currentPlayerCards] % 13 == 0) self.playerHasAce = YES;
        self.playerNumAces++;
    }
    //NSLog(@"%d",self.playerCards[currentPlayerCards]);
    self.currentPlayerCards++;*/
    
}

- (void)bust {
    
    [self reveal];
    [self lose];
    
}

- (void)stand {
    
}

- (void)double_ {
    
    //Double bet, hit once and then stand.
    self.currentChips -= self.currentBet;
    self.currentBet += self.currentBet;
    //NOTE: It is not this method's responsibilty to handle hitting and standing!  Call the individual methods yourself please
    //[self hit];
    //[self stand];
}

- (void)split {
    
    //oh my gopd
}

- (void)reveal { //Reveals the house's first card, which should be face-up when this is called
    
    int cardValue = self.houseCards[0] % 13 + 1;
    if (cardValue > 10) cardValue = 10;
    else if (cardValue == 1) cardValue = 11;    
    houseValue += cardValue;
    
    if (self.houseValue == 22) self.houseValue = 12;
    if (cardValue == 11) self.houseHasAce = YES;
    
}

- (void)playHouse {
    
    self.status = 3; //Playing the house
    
    //First we add the value of the house's face-down card, which is now face-up.
    
    [self reveal];
    
    //Play the house
    
    bool go = self.houseValue < 17 || (self.houseValue == 17 && self.houseHasAce);
    while (go) {
        int card = [self dealCard];
        self.houseCards[self.currentHouseCards] = card;
        
        int cardValue = self.houseCards[self.currentHouseCards] % 13 + 1;
        if (cardValue > 10) cardValue = 10;
        else if (cardValue == 1) cardValue = 11;
        self.houseValue += cardValue;
        
        if (card % 13 == 0) self.houseHasAce = YES;
        
        if (self.houseValue> 21 && self.houseHasAce) {
            self.houseValue -= 10;
            self.houseHasAce = NO;
        }
        
        self.currentHouseCards++;
        
        //Figure out whether to keep going or not.
        //If the highest possible value of the house's hand > 16, stop (unless the house has 16 and an ace)
        
        go = self.houseValue < 17 || (self.houseValue == 17 && self.houseHasAce);
        
        
        /*//Figure out whether to keep going or not.
        //If the house has an ace and their hand value is greater than 26 (16), stop
        //If the house has an ace and their hand value is greater than 21 (11), keep going
        //If the house has an ace and their hand value is greater than 17, stop
        //If the house has no ace and their hand value is greater than 16, stop
        if (self.houseValue > 26 && self.houseHasAce) go = NO;
        else if (self.houseValue > 17 && self.houseValue <= 21 && self.houseHasAce) go = NO;
        else if (self.houseValue > 16 && !self.houseHasAce) go = NO;
        */
    }
    
}

- (void)win {
    if (self.currentPlayerCards == 2 && self.playerValue == 21) //blackjack
        self.currentChips += ceil(self.currentBet*2.5);
    else self.currentChips += self.currentBet*2;
}

- (void)lose {
    //Nothing happens!
}

- (void)endRound {
    //Add or remove chips based on if we won
    
    if (self.playerValue > 21) {} //Player busted - lose
    else if (self.playerValue > self.houseValue || self.houseValue > 21) { //Player won or house busted - WIN
        if (self.currentPlayerCards == 2 && self.playerValue == 21) //blackjack
            self.currentChips += ceil(self.currentBet*2.5);
        else self.currentChips += self.currentBet*2;
    } else if (self.playerValue == self.houseValue) { //Tie
        self.currentChips += self.currentBet;
    } //House win - lose
    
    currentBet = 0;
    self.status = 4;
}

- (void)endGame {
    //another placeholder
}

@end
